<?php

defined('SYSPATH') or die('Access error!');

class Controller_News extends Controller_Main
{

    private $news;

    public function before()
    {
        parent::before();
        $this->news = Model::factory('news');
    }

    public function action_list()
    {
        $id = $this->request->param('id');
        if (empty($id))
        {
            $this->request->redirect(URL::site());
        }
        $category_list = $this->get_cache_file('news');
        $news_list = $this->news->get_allnews_list($id);
        if (isset($category_list[$id]))
        {
            $category_detail = $category_list[$id];
        }
        else
        {
            $category_detail = Model::factory('category')->get_row(array('id' => $id), 'name,meta_title,meta_title,meta_description,id,parent_id');
        }
        $this->template->assign('data', $category_detail);
        $this->template->assign('news_list', $news_list['list']);
        $this->template->assign('page', $news_list['page']);
        $this->template->view('news_list');
    }

    public function action_detail()
    {
        $id = intval($this->request->param('id'));
        if (empty($id))
        {
            $this->request->redirect(URL::site());
        }
        $detail = $this->news->get_detail($id);
        if (empty($detail))
        {
             $this->request->redirect(URL::site());
        }
        
        //前篇
        $pre_detail = $this->news->get_pre_news($id,$detail['category_id']);
        $this->template->assign('pre_detail',$pre_detail);
        
        //后篇
        $next_detail = $this->news->get_next_news($id,$detail['category_id']);
        $this->template->assign('next_detail',$next_detail);
        
        $this->news->update(array('clicks'=>$detail['clicks']+1),array('id'=>$id));
        $this->template->assign('data', $detail);
        
        
        
        $this->template->view('news_detail');
    }

}